import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class PlayerPanel extends JPanel implements Observer {

	private boolean areConstantsInitialized;
	private JLabel mNameLabel, mFamePointsLabel, mDevelopersOffBoardLabel,
			mOneRiceTilesLabel, mOneVillageTilesLabel, mTwoTilesLabel,
			mActionTokensLabel;

	public PlayerPanel() {
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(210, 335));
		setMinimumSize(new Dimension(210, 335));
		setMaximumSize(new Dimension(210, 335));
		setBorder(BorderFactory.createLineBorder(new Color(255, 255, 255)));

		initLayout();
	}

	private void initLayout() {
		JPanel leftPlayerInfo = new JPanel();
		leftPlayerInfo.setPreferredSize(new Dimension(120, 60));
		leftPlayerInfo.setBackground(Color.WHITE);
		this.add(leftPlayerInfo);

		mNameLabel = new JLabel();
		mNameLabel.setFont(new Font("Lucida Grande", 0, 18));
		mNameLabel.setPreferredSize(new Dimension(115, 22));
		mNameLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
		leftPlayerInfo.add(mNameLabel);

		mFamePointsLabel = new JLabel("");
		mFamePointsLabel.setFont(new Font("Lucida Grande", 1, 36));
		mFamePointsLabel.setHorizontalAlignment(SwingConstants.LEFT);
		mFamePointsLabel.setPreferredSize(new Dimension(115, 32));
		mFamePointsLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
		leftPlayerInfo.add(mFamePointsLabel);

		JSeparator jSeparator1 = new JSeparator();
		jSeparator1.setForeground(new Color(112, 102, 102));
		jSeparator1.setPreferredSize(new Dimension(180, 15));
		this.add(jSeparator1);

//		mDevelopersOffBoardLabel = newJLabel("",
//				imageSourceHashMap.get("layout_player_" + color), 90, 45);
//		this.add(mDevelopersOffBoardLabel);
//
//		mActionTokensLabel = newJLabel("",
//				imageSourceHashMap.get("layout_actionToken"), 90, 45);
//		this.add(mActionTokensLabel);
//
//		mOneRiceTilesLabel = newJLabel("",
//				imageSourceHashMap.get("layout_riceTile"), 90, 45);
//		this.add(mOneRiceTilesLabel);
//
//		mOneVillageTilesLabel = newJLabel("",
//				imageSourceHashMap.get("layout_villageTile"), 90, 45);
//		this.add(mOneVillageTilesLabel);
//
//		mTwoTilesLabel = newJLabel("",
//				imageSourceHashMap.get("layout_twoTile"), 180, 45);
//		this.add(mTwoTilesLabel);
	}

	@Override
	public void update(Observable o, Object arg) {
		DisplayablePlayer player = (DisplayablePlayer) o;
		if (!areConstantsInitialized) {
			initializeConstants(player.getName(),
					Color.getColor(player.getColor()));
		}
		updateDisplay(player.getFamePoints(), player.getDevelopersOffBoard(),
				player.getOneRiceTiles(), player.getOneVillageTiles(),
				player.getTwoTiles(), player.getActionTokens());
	}

	private void initializeConstants(String name, Color color) {
		mNameLabel.setText(name);
		mNameLabel.setForeground(color);
		areConstantsInitialized = true;
	}

	private void updateDisplay(int famePoints, int developersOffBoard,
			int oneRiceTiles, int oneVillageTiles, int twoTiles,
			int actionTokens) {
		mFamePointsLabel.setText("" + famePoints);
		mDevelopersOffBoardLabel.setText("" + developersOffBoard);
		mOneRiceTilesLabel.setText("" + oneRiceTiles);
		mOneVillageTilesLabel.setText("" + oneVillageTiles);
		mTwoTilesLabel.setText("" + twoTiles);
		mActionTokensLabel.setText("" + actionTokens);
	}

}
